var reply_collection = db.collection("reply");
var post_collection = db.collection("post");
var module_collection = db.collection("module");
(function() {
    // 构造函数
    var _con = function(info, user) {
        var self = this;
        if (info) {
            printData("正在创建回复：", info, user);
            self.type = "reply";
            // 自有id,"re_"+
            self.id = "re_" + Math.random().toString(16).substr(2, 2) + (new Date()).getTime().toString(16);
            // 回复标题
            self.title = info.title;
            // 内容
            self.content = info.content;
            // 创建者
            console.log(user);
            self.creater = user;
            self.creater_id = user._id;
            // 创建时间
            self.createtime = (new Date()).getTime();
            // 被回复者\
            console.log(info.replyer);
            info.replyer.constructor === String ? info.replyer = JSON.parse(info.replyer) : false;
            replyer = info.replyer || {};
            self.replyer = info.replyer;
            self.replyer_id = replyer._id;
            // 涉及话题
            self.post_name = info.post_name;
            self.post_id = info.post_id;
            // 涉及版块
            self._module = info._module;
            self._module_name = info._module_name;
        };
    };
    // 导出类
    module.exports = _con;

    // 创建回复
    _con.prototype.save = co.wrap(function*() {
        var self = this;
        if (!self.creater) {
            return printError("您已退出登陆，请刷新后重新登陆");
        }
        var module_doc = yield mongo.findOneByObjectId(module_collection, self._module);
        if (!module_doc) {
            return printError("回复话题的版块不存在或已被删除");
        };
        var post_doc = yield mongo.findOneByObjectId(post_collection, self.post_id);
        if (!post_doc) {
            return printError("回复的话题不存在或已被删除");
        };
        if (!self.content) {
            return printError("请填写回复内容");
        };
        var insert = yield mongo.insertOne(reply_collection, self);

        var doc = yield mongo.findOneByObjectId(reply_collection, insert.insertedId);
        return doc;
    });

    // 修改回复
    _con.prototype.update = co.wrap(function*(info) {
        var _id = info._id;
        delete info._id;
        var doc = yield mongo.findOneAndUpdateByObjectId(reply_collection, {
            _id: _id,
            data: info
        });
        doc.value ? (doc = doc.value, printSuccess("修改回复成功")) : printError("没有找到要修改的内容");
        return doc;
    });

    // 删除回复
    _con.prototype.delete = co.wrap(function*(_id) {
        printData("正在删除回复：", _id);
        var doc = yield mongo.deleteOneByObjectId(reply_collection, {
            _id: _id
        });
        return doc.value;
    });

    // 删除对应版块的回复
    _con.prototype.deleteByModule = co.wrap(function*(_module) {
        printData("正在伤处对应版块的回复：", _module);
        var result = yield mongo.delete(reply_collection, {
            query: {
                _module: _module
            }
        });
        print(result);
    });

    // 删除对应话题的回复
    _con.prototype.deleteByPost = co.wrap(function*(post_id) {
        printData("正在删除对应话题的回复：", post_id);
        var result = yield mongo.delete(reply_collection, {
            query: {
                post_id: post_id
            }
        });
        print(result);
    });

    // 获取回复列表，跟创建者，被回复者，主题
    _con.prototype.list = co.wrap(function*(info) {
        printData("正在获取回复列表：", info);
        var num = info.num || 15;
        var page = info.page || 0;
        delete info.num;
        delete info.page;
        var docs = yield mongo.find(reply_collection, {
            query: info,
            skip: page * num,
            limit: num,
            sort: {
                createtime: -1
            }
        });
        return docs;
    });

    // 获取回复统计
    _con.prototype.count = co.wrap(function*(info) {
        printData("正在获取回复统计：", info);
        return yield mongo.count(reply_collection, info || {});
    });
}());
